package cn.shenlong.portal.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.shenlong.common.Page;
import cn.shenlong.portal.domain.Store;
import cn.shenlong.portal.domain.StoreBO;
import cn.shenlong.portal.mapper.Store2tagMapper;
import cn.shenlong.portal.mapper.StoreMapper;
import cn.shenlong.portal.service.StoreService;

@Service("storeService")
public class StoreServiceImpl implements StoreService {

	@Autowired
	private StoreMapper storeMapper;
	@Autowired
	private Store2tagMapper store2tagMapper;
	
	@Override
	public int saveEntity(Store entity) {
		storeMapper.saveEntity(entity);
		return 0;
	}

	@Override
	public int updateEntity(Store entity) {
		storeMapper.updateEntity(entity);
		return 0;
	}

	@Override
	public Store findByPK(Serializable entityId) {
		if(entityId!=null){
			return storeMapper.findByPK(entityId);
		}else{
			return null;
		}
	}

	@Override
	public int deleteByPKArray(Integer[] ids) {
		if(ids!=null && ids.length>0){
			for (Integer id : ids) {
				store2tagMapper.deleteByStoreId(id);
			}
			storeMapper.deleteByPKArray(ids);
		}
		return 0;
	}

	@Override
	public Page<Store> findPageListByCriteria(Integer pageNo, Integer pageSize,
			Map<String, Object> para) {
		Page<Store> p = new Page<Store>();
		List<Store> list = storeMapper.findPageListByCriteria(para,
				initRowBounds(pageSize, pageNo, p));
		Long count = storeMapper.findCountByCriteria(para);
		p.setList(list);
		p.setTotalCount(count.intValue());
		return p;
	}

	@SuppressWarnings("rawtypes")
	private RowBounds initRowBounds(Integer pageSize, Integer pageNo, Page p) {
		RowBounds rowBounds = null;
		if (pageSize == null || pageNo == null) {
			rowBounds = new RowBounds();
		} else {
			p.setPageNo(pageNo);
			p.setPageSize(pageSize);
			rowBounds = new RowBounds(p.getFirstResult(), p.getPageSize());
		}
		return rowBounds;
	}

	@Override
	public Page<StoreBO> findPageListBOByCriteria(Integer pageNo, Integer pageSize,
			Map<String, Object> para) {
		Page<StoreBO> p = new Page<StoreBO>();
		List<StoreBO> list = storeMapper.findPageListBOByCriteria(para,
				initRowBounds(pageSize, pageNo, p));
		Long count = storeMapper.findCountBOByCriteria(para);
		p.setList(list);
		p.setTotalCount(count.intValue());
		return p;
	}

}
